/*
 * @lc app=leetcode.cn id=322 lang=java
 *
 * [322] 零钱兑换
 */


import java.util.Arrays;
// @lc code=start
class Solution {
    public int coinChange(int[] coins, int amount) {
        // 动态规划
        int[] dp = new int[amount + 1];

        // 初始化
        Arrays.fill(dp, amount + 1);

        dp[0] = 0;

        for(int i = 0; i <= amount; i++) {
            for(int c : coins) {
                if(i - c < 0) {
                    continue;
                }
                dp[i] = Math.min(dp[i], dp[i - c] + 1);
            }
        }
        return dp[amount] == amount + 1 ? -1 : dp[amount];
    }
}
// @lc code=end

